<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card :body-style="{ padding: '20px' }">
        <el-row type="flex" justify="end">
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="addPermission(1,0)">添加菜单</el-button>
        </el-row>
        <el-table :data="list" style="width: 100%" header-cell-class-name="gray" row-key="id">
          <el-table-column prop="name" label="菜单名称" width="200" />
          <el-table-column prop="code" label="权限标识" width="220" />
          <el-table-column prop="description" label="描述" width="240" />
          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button v-if="row.type===1" type="text" @click="addPermission(2,row.id)">添加</el-button>
              <el-button type="text" @click="updatePermission(row.id)">编辑</el-button>
              <el-button type="text" @click="delPermission(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 添加权限点 -->
      <el-dialog :visible="isShow" :title="showText" @close="clear">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px" size="normal" style="padding-top:20px">
          <el-form-item label="权限名称" style="width:90%" prop="name">
            <el-input v-model="form.name" />
          </el-form-item>
          <el-form-item label="权限标识" style="width:90%" prop="code">
            <el-input v-model="form.code" />
          </el-form-item>
          <el-form-item label="权限描述" style="width:90%">
            <el-input v-model="form.description" />
          </el-form-item>
          <el-form-item label="企业可见" style="width:90%">
            <el-switch
              v-model="form.enVisible"
              active-text="可见"
              inactive-text="不可见"
              active-color="#13ce66"
              inactive-color="#dcdfe6"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
          <el-form-item style="width:90%">
            <el-row type="flex" justify="center" align="bottom">
              <el-button type="primary" @click="BtnPermission">确定</el-button>
              <el-button @click="clear">取消</el-button>
            </el-row>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermissionList, getPermissionDetail, addPermission, updatePermission, delPermission } from '@/api/permission'
import { tranListToTreeData } from '@/utils'
export default {
  data() {
    return {
      tableData: [],
      list: [],
      isShow: false,
      form: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 开启
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    showText() {
      return this.form.id ? '编辑' : '新增'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      this.tableData = await getPermissionList()
      // console.log(this.tableData)
      this.list = tranListToTreeData(this.tableData, '0')
      // console.log(this.list)
    },
    async delPermission(id) {
      this.$confirm('是否删除该条权限', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delPermission(id)
        this.$message.success('删除成功')
        this.getPermissionList()
      }
      )
    },
    addPermission(type, pid) {
      this.isShow = true
      this.form.type = type
      this.form.pid = pid
    },
    async BtnPermission() {
      this.$refs.form.validate(async(valid) => {
        if (valid) {
          if (this.form.id) {
            await updatePermission(this.form)
            this.$message.success('编辑权限成功')
          } else {
            await addPermission(this.form)
            this.$message.success('新增权限成功')
          }
          this.getPermissionList()
          this.isShow = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    clear() {
      this.$refs.form.resetFields()
      this.form = {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 开启
      }
      this.isShow = false
    },
    async updatePermission(id) {
      this.isShow = true
      this.form = await getPermissionDetail(id)
      console.log(this.form)
    }
  }
}
</script>

<style>
.gray {
  background-color: #fafafa !important;
}
</style>
